package com.wx.wxapp.service;

import com.wx.wxapp.dao.PetMapper;
import com.wx.wxapp.dao.UserMapper;
import com.wx.wxapp.entity.Pet;
import com.wx.wxapp.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PetService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    public List<Pet> getAllPet(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);

        List<Pet> pets = petMapper.selectAll();
        sqlSession.close();
        return pets;
    }

    public List<Pet> getPet(String openid){
        if(openid==null){
            
        }
        SqlSession sqlSession  = sqlSessionFactory.openSession();
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectByOpenid(openid);
        int id = user.getId();
        List<Pet> pets = petMapper.selectByUserId(id);

        sqlSession.close();
        return pets;
    }
}
